﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;

public partial class admin_manageSlide : System.Web.UI.Page
{
    db_slide slide = new db_slide();
    static DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("inc/checkPurview.aspx");  // 此处用户权限验证

        if (!Page.IsPostBack)
        {
            ds = slide.selectSlideALL();
            bind(ds);
        }

    }

    public void bind(DataSet ds)
    {
        try
        {

            lbl_titelID_1.Text = ds.Tables[0].Rows[0][0].ToString();
            tb_pic_1.Text = ds.Tables[0].Rows[0][1].ToString();
            tb_title_1.Text = ds.Tables[0].Rows[0][2].ToString();
            tb_text_1.Text = ds.Tables[0].Rows[0][3].ToString();
            tb_url_1.Text = ds.Tables[0].Rows[0][4].ToString();

            lbl_titelID_2.Text = ds.Tables[0].Rows[1][0].ToString();
            tb_pic_2.Text = ds.Tables[0].Rows[1][1].ToString();
            tb_title_2.Text = ds.Tables[0].Rows[1][2].ToString();
            tb_text_2.Text = ds.Tables[0].Rows[1][3].ToString();
            tb_url_2.Text = ds.Tables[0].Rows[1][4].ToString();

            lbl_titelID_3.Text = ds.Tables[0].Rows[2][0].ToString();
            tb_pic_3.Text = ds.Tables[0].Rows[2][1].ToString();
            tb_title_3.Text = ds.Tables[0].Rows[2][2].ToString();
            tb_text_3.Text = ds.Tables[0].Rows[2][3].ToString();
            tb_url_3.Text = ds.Tables[0].Rows[2][4].ToString();

            lbl_titelID_4.Text = ds.Tables[0].Rows[3][0].ToString();
            tb_pic_4.Text = ds.Tables[0].Rows[3][1].ToString();
            tb_title_4.Text = ds.Tables[0].Rows[3][2].ToString();
            tb_text_4.Text = ds.Tables[0].Rows[3][3].ToString();
            tb_url_4.Text = ds.Tables[0].Rows[3][4].ToString();

            lbl_titelID_5.Text = ds.Tables[0].Rows[4][0].ToString();
            tb_pic_5.Text = ds.Tables[0].Rows[4][1].ToString();
            tb_title_5.Text = ds.Tables[0].Rows[4][2].ToString();
            tb_text_5.Text = ds.Tables[0].Rows[4][3].ToString();
            tb_url_5.Text = ds.Tables[0].Rows[4][4].ToString();
        }
        catch { }
    }

    public void bind_create(DataSet ds)
    {
        try
        {
            tb_pic_1.Text = ds.Tables[0].Rows[0][7].ToString();
            tb_title_1.Text = ds.Tables[0].Rows[0][1].ToString();
            string nohtml = Tools.NoHTML(ds.Tables[0].Rows[0][2].ToString());
            if (nohtml.Length > 20)
            {
                tb_text_1.Text = nohtml.Substring(0, 20) + "……";
            }
            else
            {
                tb_text_1.Text = nohtml + "……";
            }
            tb_url_1.Text = "/news.aspx?id=" + ds.Tables[0].Rows[0][0].ToString();
        }
        catch
        {
            tb_pic_1.Text = "";
            tb_title_1.Text = "";
            tb_text_1.Text = "";
            tb_url_1.Text = "";
        }

        try
        {
            tb_pic_2.Text = ds.Tables[0].Rows[1][7].ToString();
            tb_title_2.Text = ds.Tables[0].Rows[1][1].ToString();
            string nohtml = Tools.NoHTML(ds.Tables[0].Rows[1][2].ToString());
            if (nohtml.Length > 20)
            {
                tb_text_2.Text = nohtml.Substring(0, 20) + "……";
            }
            else
            {
                tb_text_2.Text = nohtml + "……";
            }
            tb_url_2.Text = "/news.aspx?id=" + ds.Tables[0].Rows[1][0].ToString();
        }
        catch
        {
            tb_pic_2.Text = "";
            tb_title_2.Text = "";
            tb_text_2.Text = "";
            tb_url_2.Text = "";
        }

        try
        {
            tb_pic_3.Text = ds.Tables[0].Rows[2][7].ToString();
            tb_title_3.Text = ds.Tables[0].Rows[2][1].ToString();
            string nohtml = Tools.NoHTML(ds.Tables[0].Rows[2][2].ToString());
            if (nohtml.Length > 20)
            {
                tb_text_3.Text = nohtml.Substring(0, 20) + "……";
            }
            else
            {
                tb_text_3.Text = nohtml + "……";
            }
            tb_url_3.Text = "/news.aspx?id=" + ds.Tables[0].Rows[2][0].ToString();
        }
        catch
        {
            tb_pic_3.Text = "";
            tb_title_3.Text = "";
            tb_text_3.Text = "";
            tb_url_3.Text = "";
        }

        try
        {
            tb_pic_4.Text = ds.Tables[0].Rows[3][7].ToString();
            tb_title_4.Text = ds.Tables[0].Rows[3][1].ToString();
            string nohtml = Tools.NoHTML(ds.Tables[0].Rows[3][2].ToString());
            if (nohtml.Length > 20)
            {
                tb_text_4.Text = nohtml.Substring(0, 20) + "……";
            }
            else
            {
                tb_text_4.Text = nohtml + "……";
            }
            tb_url_4.Text = "/news.aspx?id=" + ds.Tables[0].Rows[3][0].ToString();
        }
        catch
        {
            tb_pic_4.Text = "";
            tb_title_4.Text = "";
            tb_text_4.Text = "";
            tb_url_4.Text = "";
        }

        try
        {
            tb_pic_5.Text = ds.Tables[0].Rows[4][7].ToString();
            tb_title_5.Text = ds.Tables[0].Rows[4][1].ToString();
            string nohtml = Tools.NoHTML(ds.Tables[0].Rows[4][2].ToString());
            if (nohtml.Length > 20)
            {
                tb_text_5.Text = nohtml.Substring(0, 20) + "……";
            }
            else
            {
                tb_text_5.Text = nohtml + "……";
            }
            tb_url_5.Text = "/news.aspx?id=" + ds.Tables[0].Rows[4][0].ToString();
        }
        catch
        {
            tb_pic_5.Text = "";
            tb_title_5.Text = "";
            tb_text_5.Text = "";
            tb_url_5.Text = "";
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        //bind();
        //string i = "";
        if (slide.updateSlide(lbl_titelID_1.Text, tb_pic_1.Text, tb_title_1.Text, tb_text_1.Text, tb_url_1.Text))
        {
            if (slide.updateSlide(lbl_titelID_2.Text, tb_pic_2.Text, tb_title_2.Text, tb_text_2.Text, tb_url_2.Text))
            {
                if (slide.updateSlide(lbl_titelID_3.Text, tb_pic_3.Text, tb_title_3.Text, tb_text_3.Text, tb_url_3.Text))
                {
                    if (slide.updateSlide(lbl_titelID_4.Text, tb_pic_4.Text, tb_title_4.Text, tb_text_4.Text, tb_url_4.Text))
                    {
                        if (slide.updateSlide(lbl_titelID_5.Text, tb_pic_5.Text, tb_title_5.Text, tb_text_5.Text, tb_url_5.Text))
                        {
                            Response.Write("<script language=javascript>alert('幻灯片修改成功！');</script>");
                        }
                        else
                        {
                            Response.Write("<script language=javascript>alert('幻灯片 5 修改失败！');</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('幻灯片 4 修改失败！');</script>");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('幻灯片 3 修改失败！');</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('幻灯片 2 修改失败！');</script>");
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('幻灯片 1 修改失败！');</script>");
        }
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
        bind(ds);
    }
    protected void btn_create_Click(object sender, EventArgs e)
    {
        db_news news = new db_news();
        DataSet newDs = news.selectNewsALLSlide();
        bind_create(newDs);
    }
}